import React from 'react';
import { Http, BeanUtils } from './../../common/Utils';
import { CrudTable, Toolbar, EditForm, Crud } from './../../common/Crud';
import { Button, message, Row, Col } from 'antd';

class ChangePwd extends React.Component {

    state = {
        fields: [
            {
                name: 'oldPassword',
                label: '旧密码',
                type: 'text',
                span: 24,
                props: {
                    type: 'password'
                },
                option: {
                    rules: [
                        { required: true, message: '请输入旧密码' },
                        { min: 6, max: 18, message: '只能输入6-18位' }
                    ]
                }
            }, {
                name: 'newPassword',
                label: '新密码',
                type: 'text',
                span: 24,
                props: {
                    type: 'password'
                },
                option: {
                    rules: [
                        { required: true, message: '请输入新密码' },
                        { min: 6, max: 18, message: '只能输入6-18位' }
                    ]
                }
            }, {
                name: 'newPassword2',
                label: '确认新密码',
                type: 'text',
                span: 24,
                props: {
                    type: 'password'
                },
                option: {
                    rules: [
                        { required: true, message: '请再次输入新密码' },
                        { min: 6, max: 18, message: '只能输入6-18位' }
                    ]
                }
            }
        ]
    }

    onUpdate() {

        window.form = this.saveForm;

        var data = this.saveForm.form.getFieldsValue();

        if (!data.oldPassword) {
            message.error('请输入旧密码');
            return;
        }

        if (!data.newPassword) {
            message.error('请输入新密码');
            return;
        }

        if (data.newPassword != data.newPassword2) {
            message.error('两次新密码不一致');
            return;
        }

        this.saveForm.submit({
            url: '/api/manager/changePwd.json'
        }, () => {
            message.info('密码修改成功');
            this.saveForm.form.setFieldsValue({
                oldPassword: '',
                newPassword: '',
                newPassword2: ''
            });
        });
    }

    render() {
        return (
            <Row type="flex" justify="center">
                <Col span={8}>
                    <EditForm ref={(saveForm) => this.saveForm = saveForm} fields={this.state.fields} />
                    <div style={{width: '100%', height: '50px', textAlign: 'center', margin: '10px 0'}}>
                        <Button type="primary" onClick={() => this.onUpdate()}>确认</Button>
                    </div>
                </Col>
            </Row>
        );
    }
}


export default ChangePwd;